package frontdesk

import (
	"individual/controllers"
	"individual/models"
	"math"
	"time"
)

type LeaveWordController struct {
	controllers.BaseController
}

// GetAllLeaveWordPass
// @Title	GetAllLeaveWordPass
// @Description 查询所有通过的留言
// @Success 201 获取所有通过的留言成功
// @Failure 403 获取所有通过的留言失败
// @router /getAllLeaveWordPass/ [get]
func (c *LeaveWordController) GetAllLeaveWordPass() {
	var leavewords []models.LeaveWord
	all, err := c.O.QueryTable(new(models.LeaveWord).TableName()).Filter("Pass", 1).OrderBy("-id").All(&leavewords)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "查询所有留言失败")
		c.ServeJSON()
		return
	}
	for i := 0; i < int(all); i++ {
		_, err = c.O.QueryTable(new(models.LeaveWordTwo).TableName()).Filter("LeaveWord", leavewords[i].Id).Filter("Pass", 1).All(&leavewords[i].LeaveWordTwo)
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "查询所有留言失败")
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = models.GetRevaOk(leavewords)
	c.ServeJSON()
}

// AddLeaveWord
// @Title	AddLeaveWord
// @Description 增加一级留言
// @Param name		formdata	string true	"名称"
// @Param email		formdata	string true	"email"
// @Param home		formdata	string true	"主页"
// @Param word		formdata	string true	"留言"
// @Param pass		formdata	int    true	"是否通过，1-通过，0-不通过"
// @Param author	formdata	int	   true	"是否是作者，1-是，0-不是"
// @Param photo		formdata	string true	"头像"
// @Success 201 增加一级留言成功
// @Failure 403 增加一级留言失败
// @router /addLeaveWord/ [post]
func (c *LeaveWordController) AddLeaveWord() {
	name := c.GetString("name")
	email := c.GetString("email")
	home := c.GetString("home")
	word := c.GetString("word")
	photo := c.GetString("photo")
	pass, err := c.GetInt8("pass")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "状态格式错误")
		c.ServeJSON()
		return
	}
	author, err := c.GetInt8("author")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "是否是作者格式错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Insert(&models.LeaveWord{Time: time.Now(), Name: name, Email: email, Homepage: home, Word: word, Photo: photo, Pass: pass, IsAuthor: author})
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "留言失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("留言成功")
	c.ServeJSON()
}

// AddLeaveWordTwo
// @Title	AddLeaveWordTwo
// @Description 增加二级留言
// @Param name		formdata	string true	"名称"
// @Param email		formdata	string true	"email"
// @Param home		formdata	string true	"主页"
// @Param word		formdata	string true	"留言"
// @Param pass		formdata	int    true	"是否通过，1-通过，0-不通过"
// @Param author	formdata	int	   true	"是否是作者，1-是，0-不是"
// @Param id		formdata	int	   true	"一级留言的id"
// @Param photo		formdata	string true	"头像"
// @Param reply		formdata	string true	"回复"
// @Success 201 增加二级留言成功
// @Failure 403 增加二级留言失败
// @router /addLeaveWordTwo/ [post]
func (c *LeaveWordController) AddLeaveWordTwo() {
	name := c.GetString("name")
	email := c.GetString("email")
	home := c.GetString("home")
	word := c.GetString("word")
	reply := c.GetString("reply")
	photo := c.GetString("photo")
	pass, err := c.GetInt8("pass")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "状态格式错误")
		c.ServeJSON()
		return
	}
	id, err := c.GetInt64("id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id格式错误")
		c.ServeJSON()
		return
	}
	author, err := c.GetInt8("author")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "是否是作者格式错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Insert(&models.LeaveWordTwo{Reply: reply, Time: time.Now(), LeaveWord: &models.LeaveWord{Id: id}, Name: name, Email: email, Homepage: home, Word: word, Photo: photo, Pass: pass, IsAuthor: author})
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "留言失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("留言成功")
	c.ServeJSON()
}

// GetLeaveWordByPagePass
// @Title GetLeaveWordByPagePass
// @Description 分页获取留言
// @Param pagesize	path	int true	"pagesize"
// @Param page	path	int true	"page"
// @Success 201 获取成功
// @Failure 403 获取失败
// @router /getLeaveWordByPagePass/ [get]
func (c *LeaveWordController) GetLeaveWordByPagePass() {
	qs := c.O.QueryTable(new(models.LeaveWord).TableName()).Filter("Pass", 1).OrderBy("-id")
	count, err := qs.Count()
	pageSetNum, err := c.GetInt("pagesize")
	if err != nil {
		pageSetNum = 1
	}
	//	总页数
	pageCount := math.Ceil(float64(count) / float64(pageSetNum))
	//	获取当前页码
	pageNum, err := c.GetInt("page")
	if err != nil {
		pageNum = 1
	}
	var leaveWords []models.LeaveWord
	all, err := qs.Limit(pageSetNum, pageSetNum*(pageNum-1)).All(&leaveWords)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "错误")
		c.ServeJSON()
		return
	}
	for i := 0; i < int(all); i++ {
		_, err = c.O.QueryTable(new(models.LeaveWordTwo).TableName()).Filter("LeaveWord", leaveWords[i].Id).Filter("Pass", 1).OrderBy("id").All(&leaveWords[i].LeaveWordTwo)
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "查询所有留言失败")
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"num":        count,
		"pageNum":    pageNum,
		"pageCount":  pageCount,
		"leaveWords": leaveWords,
	})
	c.ServeJSON()
}
